package com.spit.fam;

import android.content.Context;
import android.util.Log;

import com.orhanobut.hawk.Hawk;
import com.spit.fam.WebService.RetrofitClient;
import com.spit.fam.nike.HttpsSSLHelper;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okio.Buffer;

public class UnsafeOkHttpClient {
    public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            Dispatcher dispatcher = new Dispatcher();
            dispatcher.setMaxRequests(100); // 最大并发请求数
            dispatcher.setMaxRequestsPerHost(100); // 每个主机的最大并发请求数

            String token = Hawk.get(InternalStorage.OFFLINE_CACHE.USER_TOKEN);
//            Log.e("token---------", token + "");
            if (token != null){
                Interceptor authInterceptor = chain -> {
                    Request originalRequest = chain.request();
                    Request requestWithAuth = originalRequest.newBuilder()
                            .header("Authorization", token)
                            .build();
                    return chain.proceed(requestWithAuth);
                };
                builder.addInterceptor(authInterceptor);
            }

            X509TrustManager x509TrustManager = HttpsSSLHelper.createTrustCustomTrustManager(HttpsSSLHelper.getInputStreamFromString(CER_HYH));
//            X509TrustManager x509TrustManager = HttpsSSLHelper.createTrustAllTrustManager();

            OkHttpClient okHttpClient = builder
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS).dispatcher(dispatcher)
                    .sslSocketFactory(HttpsSSLHelper.createSSLSocketFactory(x509TrustManager),x509TrustManager)
                    .hostnameVerifier(new HttpsSSLHelper.TrustAllHostnameVerifier())
                    .build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static String CER_HYH = "-----BEGIN CERTIFICATE-----\n" +
            "MIIDEDCCAfigAwIBAgIUbm7yLv8OTgicQBOhZ1KRd4pSb9wwDQYJKoZIhvcNAQEL\n" +
            "BQAwFzEVMBMGA1UEAwwMMTkyLjE2OC4xLjIwMB4XDTI0MTAxODA3MzMxOVoXDTI3\n" +
            "MTAxODA3MzMxOVowFzEVMBMGA1UEAwwMMTkyLjE2OC4xLjIwMIIBIjANBgkqhkiG\n" +
            "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjyzOqozzipGcduCeogXN2hrULEFbaLssrOh/\n" +
            "f1nGKlue69uO4gP4385Pnkvj0AjrmpAOZwjl7h2F4R4ly9C7aHtCecSTsNBYfJ8B\n" +
            "AFBD+j2NvuJdSReqc+QEVWWb/zJ9+EvbXTwWF0OeFs9ALgMcsDVaOqyOu7wMz2sS\n" +
            "NFFAvSof27Bri2G8efw91UYIva7mDPHln+J0B2Wj9wrD/DpZazqNxdy9xd/6eZdT\n" +
            "M/r0G+K72jh2vPPtqDWkYgeSF3qCwPDXqGdEfayLOa6jJzjoQqmXrpeYxPdJoml3\n" +
            "+VYttspcVyG5MbMf/hUvkXvwI5tdiMZ1sNKp6cZEgbBReYJWBQIDAQABo1QwUjAP\n" +
            "BgNVHREECDAGhwTAqAEUMAsGA1UdDwQEAwIFoDATBgNVHSUEDDAKBggrBgEFBQcD\n" +
            "ATAdBgNVHQ4EFgQUdHsvN+yFE1k2+OWVSwe3vjGfyBUwDQYJKoZIhvcNAQELBQAD\n" +
            "ggEBAGyOfaawwIz44SGfRHF/ZCJlagLP/QvRaxOO99hTcDIe4WTtjV96xTT0mDAM\n" +
            "yZYapirM5SJP/TBVXjWSiOaZF4q2kf9eTZ8gHTaO9F9Tua7CaFOfZ+DHIFCThooS\n" +
            "H+U9l56D1wqzgX36qM8oG/BOodc3h+BvryynarAKSN50tKIYBzwXKvgTTTTBModS\n" +
            "sH6wrX9zeiV4h5X6vFeZRFvUoxKWKdLyEMC4ls1ApNZ0fj37xDd3FXSYsc3t1yrr\n" +
            "Mose2KQqvdsnQuGNhfwHh7Pa78t3gGxaPJL4rzvVC6XTKeksCbaJVDjRD0U+m/VA\n" +
            "g8GxoQZtZqBx+0ILu4io0CgFSXY=\n" +
            "-----END CERTIFICATE-----\n";
}
